package com.hna.eking.AirlineServer;

import javax.servlet.MultipartConfigElement;

import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.MultipartAutoConfiguration;
import org.springframework.boot.context.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.DispatcherServlet;

import com.hna.eking.AirlineServer.Platform.FileUpload.CommonsMultipartResolverExt;

/**
 * @author Merin
 * 排除自动配置：MultipartAutoConfiguration
 */
//@SpringBootApplication(exclude={MultipartAutoConfiguration.class})// 升级2.0.0后找不到这个类了
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.hna.eking.AirlineServer.Mapper")
public class Application {
	
	/*
	 * Spring Boot建议将我们main方法所在的这个主要的配置类配置在根包名下。
	 */
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
/**  
 * 	使用springboot的自动配置时，必须开启，否则报错“Required request part 'upfile' is not present”
 *	自定义CommonsMultipartResolver时，不能开启，否则会进入两次dispatcherservlet，
 *		导致第二次的解析request时获取不到multifile，报错“Required request part 'upfile' is not present”
**/
//    @Bean
//    public MultipartConfigElement multipartConfigElement() {
//        MultipartConfigFactory factory = new MultipartConfigFactory();
//        // 设置文件大小限制 ,超出设置页面会抛出异常信息，
//        // 这样在文件上传的地方就需要进行异常信息的处理了;
//        factory.setMaxFileSize("10KB"); // KB,MB
//        /// 设置总上传数据总大小
//        factory.setMaxRequestSize("512MB");
//        // Sets the directory location where files will be stored.
//        // factory.setLocation("路径地址");
//        return factory.createMultipartConfig();
//    }

    //tomcatEmbedded这段代码是为了解决，上传文件超过文件大小限制出现连接重置的问题。此异常内容GlobalException也捕获不到。
    @Bean
    public TomcatEmbeddedServletContainerFactory tomcatEmbedded() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
            if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol<?>)) {
                //-1 means unlimited
                ((AbstractHttp11Protocol<?>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
            }
        });
        return tomcat;
    }

	

}
